package org.jeecg.modules.deepseek.tools;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.QueryWrapperBuilder;
import org.jeecg.common.util.deepseek.SqlQueryChecker;
import org.jeecg.modules.deepseek.pojo.SQLType;
import org.jeecg.modules.demo.wuyou.entity.Sitemap;
import org.jeecg.modules.demo.wuyou.entity.WebTeammx;
import org.jeecg.modules.demo.wuyou.service.ISitemapService;
import org.jeecg.modules.demo.wuyou.service.IWebTeammxService;
import org.jeecg.modules.demo.wuyou.service.IWebWyjbinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Configuration
public class WebWyjbinfoTools {


    @Autowired
    ISitemapService sitemapService;

   @Schema(description = "查询案例所需信息所需字段，包括查询条件和排序条件。把放在SelectSql字段中，只查询主要字段，不需要查询所有字段")
    public record QueryWebjbinfo(
           @Schema(description = "查询SQL,需要把几张表关联起来不要查询targename这个字段。示例关联关系" +
                   "SELECTw.id, w.row_id, w.NAME, w.CASTITLE, w.NAMETIME, w.create_time, w.CASNAME, w.TEACHERNAME, w.ADMISSSCHOOL, w.ENRMAJOR, w.OUTSCHOOL, w.SCHOOLMAJOR, w.CASBACK, w.MAINEXPERIENCE, w.IMAGEURL, w.PROJECTPRICE, w.PROJECTTIME, w.SHOOLTIME, w.APPLYASK, w.LANGUAGEASK, w.SUCCESSCASE, w.APPLYSUCCESS," +
                   " w.FAILCASE, w.seq_id, w.update_time, w.ENRMAJORID, s.SCHOOLNAME, t.TEAMMXNAME FROM\n" +
                   "                 web_wyjbinfo w LEFT JOIN web_hotshoolmx s ON w.ADMISSSCHOOL = s.id LEFT JOIN web_teammx t ON w.TEACHERNAME = t.id\n" +
                   "  ORDER BY w.NAMETIME DESC LIMIT 20;")String selectSql,

           @Schema(description = "案例表结构：create table web_wyjbinfo\n" +
                   "(\n" +
                   "    id             int(128)      not null\n" +
                   "        primary key,\n" +
                   "    row_id         varchar(255)  null comment 'row_id',\n" +
                   "    NAME           varchar(200)  not null comment '标题',\n" +
                   "    CASTITLE       varchar(200)  null comment '副标题',\n" +
                   "    NAMETIME       date          not null comment '时间',\n" +
                   "    create_time    datetime      null comment '插入时间',\n" +
                   "    CASNAME        varchar(100)  null comment '学生姓名',\n" +
                   "    TEACHERNAME    varchar(100)  null comment '服务导师ID',\n" +
                   "    ADMISSSCHOOL   varchar(100)  null comment '录取学校ID',\n" +
                   "    ENRMAJOR       varchar(100)  null comment '录取专业',\n" +
                   "    OUTSCHOOL      varchar(100)  null comment '毕业学校',\n" +
                   "    SCHOOLMAJOR    varchar(100)  null comment '本科专业',\n" +
                   "    CASBACK        varchar(100)  null comment '基本背景',\n" +
                   "    MAINEXPERIENCE varchar(2000) null comment '主要经历',\n" +
                   "    IMAGEURL       varchar(255)  null comment 'Offer展示',\n" +
                   "    PROJECTPRICE   varchar(255)  null comment '项目学费',\n" +
                   "    PROJECTTIME    varchar(255)  null comment '项目时长',\n" +
                   "    SHOOLTIME      varchar(255)  null comment '入学时间',\n" +
                   "    APPLYASK       varchar(2000) null comment '申请要求',\n" +
                   "    LANGUAGEASK    varchar(2000) null comment '语言要求',\n" +
                   "    targename      text          null comment '培养目标',\n" +
                   "    SUCCESSCASE    varchar(20)   null comment '成功案例',\n" +
                   "    APPLYSUCCESS   varchar(20)   null comment '申请成功率',\n" +
                   "    FAILCASE       varchar(20)   null comment '失败案例',\n" +
                   "    seq_id         int(10)       null comment '排序名称',\n" +
                   "    update_time    datetime      null comment '更新日期',\n" +
                   "    ENRMAJORID     int           null\n" +
                   ")\n" +
                   "    comment '无忧捷报详细信息' collate = utf8mb4_unicode_ci;" +
                   "学校表详情 create table web_hotshoolmx\n" +
                   "(\n" +
                   "    id              varchar(100)             not null\n" +
                   "        primary key,\n" +
                   "    hotshool_id     varchar(100)             not null comment 'hotshoolId',\n" +
                   "    row_id          varchar(255)             null comment 'row_id',\n" +
                   "    SCHOOLNAME      varchar(50)   default '' null comment '学校名称',\n" +
                   "    ENGTHSCHOOLNAME varchar(100)  default '' null comment '英文名称',\n" +
                   "    IMAGEURL        varchar(255)  default '' null comment '图片地址',\n" +
                   "    SCHOOLTOP       varchar(100)  default '' null comment '学校排名',\n" +
                   "    SCHOOLTYPE      varchar(100)  default '' null comment '学校类型',\n" +
                   "    SCHOOLPER       varchar(100)  default '' null comment '国际生比',\n" +
                   "    SCHOOLIN        varchar(100)  default '' null comment '录取率',\n" +
                   "    SCHOOLOUT       varchar(100)  default '' null comment '毕业率',\n" +
                   "    SCHOOLPRICE     varchar(100)  default '' null comment '学费',\n" +
                   "    SEQ_ID          int                      null comment '排序名称',\n" +
                   "    explain_text    text                     null comment '学校简介',\n" +
                   "    APPLAYNUMBER    varchar(255)             null comment '申请人数',\n" +
                   "    SCHOOLADDRESS   varchar(255)  default '' null comment '学校地址',\n" +
                   "    SCHOOLGUANGWA   varchar(255)             null comment '学校官网',\n" +
                   "    SHIJEUSNEWS     int                      null comment '世界USNEWS排名',\n" +
                   "    SHIJIEQS        int                      null comment '世界(QS)排名',\n" +
                   "    JIUYELV         varchar(50)   default '' null comment '就业率',\n" +
                   "    BIYXINZI        varchar(50)   default '' null comment '毕业薪资',\n" +
                   "    XUESZONGSU      varchar(50)   default '' null comment '学生总数量',\n" +
                   "    JIANGJIUSZONGS  varchar(50)   default '' null comment '研究生数量',\n" +
                   "    BENKEZONGS      varchar(50)   default '' null comment '本科生数量',\n" +
                   "    GUOJIXUESBL     varchar(50)   default '' null comment '国际学生比例',\n" +
                   "    NANNVBL         varchar(50)   default '' null comment '男女比例',\n" +
                   "    QIANGSHIZY      varchar(1000) default '' null comment '强势专业',\n" +
                   "    REMENGZY        varchar(255)  default '' null comment '热门专业',\n" +
                   "    XUANZEYXLY      text                     null comment '选择该校的理由',\n" +
                   "    DILIWEIZHI      text                     null comment '地理位置',\n" +
                   "    XUEXIAOLS       text                     null comment '学校历史',\n" +
                   "    XUEXIAOHJ       text                     null comment '校园环境',\n" +
                   "    XUEXIAOTS       text                     null comment '学校特色',\n" +
                   "    YUANXISHEZHI    text                     null comment '院系设置',\n" +
                   "    JIAOYETES       text                     null comment '教学特色',\n" +
                   "    ZSBXX           text                     null comment '招生办信息',\n" +
                   "    id_order_shool  int(10)                  null comment 'idOrderShool'\n" +
                   ")\n" +
                   "    comment '热门院校及专业详情' collate = utf8mb4_unicode_ci;" +
                   " 顾问（老师）表详情create table web_teammx\n" +
                   "(\n" +
                   "    id              varchar(100)            not null\n" +
                   "        primary key,\n" +
                   "    row_id          varchar(255)            null comment 'row_id',\n" +
                   "    team_id         varchar(100)            not null comment 'teamId',\n" +
                   "    imageurl        text                    null comment '图片地址',\n" +
                   "    TEAMMXNAME      varchar(50)  default '' null comment '团队人员名称',\n" +
                   "    AREAADVISER     varchar(50)  default '' null comment '地区顾问',\n" +
                   "    IMPORTAREA      varchar(50)  default '' null comment '负责地区',\n" +
                   "    SERVICEINFO     varchar(500) default '' null comment '咨询信息',\n" +
                   "    SEQ_ID          int                     not null comment '排序名称',\n" +
                   "    CASENUMBER      int                     null comment '案例总数',\n" +
                   "    PERSONNUMBER    int                     null comment '咨询认识',\n" +
                   "    WORKTIME        int                     null comment '工作年限',\n" +
                   "    jiangjie        text                    null comment '个人简介',\n" +
                   "    JAOYUBEIJ       varchar(500) default '' null comment '教育背景',\n" +
                   "    DICTIONARIES_ID varchar(100) default '' null comment '顾问级别'\n" +
                   ")\n" +
                   "    comment '团队信息明细' collate = utf8mb4_unicode_ci;")String tableStructure,
           @Schema(description = "案例表示例数据： {\n" +
                   "    \"id\": 171,\n" +
                   "    \"row_id\": \"2dc599e7-b01a-4e32-9cfb-1684261b2e9c\",\n" +
                   "    \"NAME\": \"2025香港录取 | 均分87+，喜提香港理工大学、香港城市大学offer！\",\n" +
                   "    \"CASTITLE\": \"2025香港录取 | 均分87+，喜提香港理工大学、香港城市大学offer！\",\n" +
                   "    \"NAMETIME\": \"2025-01-13\",\n" +
                   "    \"create_time\": null,\n" +
                   "    \"CASNAME\": \"江同学\",\n" +
                   "    \"TEACHERNAME\": \"ab2050a7-dc0e-4488-a858-761ef0573340\",\n" +
                   "    \"ADMISSSCHOOL\": \"518b0320-3759-474a-b4a6-7db457a895ed\",\n" +
                   "    \"ENRMAJOR\": \"\",\n" +
                   "    \"OUTSCHOOL\": \"211\",\n" +
                   "    \"SCHOOLMAJOR\": \"会计\",\n" +
                   "    \"CASBACK\": \"均分87+，雅思6.5，GRE310\",\n" +
                   "    \"MAINEXPERIENCE\": \"\",\n" +
                   "    \"IMAGEURL\": \"https://scrm.51liuxue.com/file/mdpic/0ca07666-5a5d-40c5-a468-1ab4bfe3fab3/fa8c6611-899c-4f35-84fc-d374082e0258/6708fd829c0e3dc1dfa21128/20250113/8UcM60dc7V0l268rb0dgaIf8081N2nbA5ocB9CbM3rafdHeW8EfBas99e2bo1S3x.jpg\",\n" +
                   "    \"PROJECTPRICE\": \"eec7194d-c6e5-4208-9b4c-e6393db76620\",\n" +
                   "    \"PROJECTTIME\": \"\",\n" +
                   "    \"SHOOLTIME\": \"\",\n" +
                   "    \"APPLYASK\": \"\",\n" +
                   "    \"LANGUAGEASK\": \"\",\n" +
                   "    \"targename\": \" 富文本内容\",\n" +
                   "    \"SUCCESSCASE\": \"\",\n" +
                   "    \"APPLYSUCCESS\": \"\",\n" +
                   "    \"FAILCASE\": \"\",\n" +
                   "    \"seq_id\": 1,\n" +
                   "    \"update_time\": null,\n" +
                   "    \"ENRMAJORID\": null\n" +
                   "  } ")String sampleData


   ) {


    }

    @Bean
    @Description("根据条件生成SQL，查询案例（offer）信息")
    public Function<QueryWebjbinfo, List<Map<String, Object>>> getWebjbinfoBySelected() {
        return request -> {
            try {
                String s = SqlQueryChecker.validateAndModifySql(request.selectSql, new String[]{"web_wyjbinfo", "web_hotshoolmx", "web_teammx"});


//                List<Map<String, Object>> maps = sitemapService.GetByCondition("select db_field_name,db_field_txt\n" +
//                        "from onl_cgform_field left join onl_cgform_head oci on oci.id = onl_cgform_field.cgform_head_id\n" +
//                        "where oci.table_name in('web_wyjbinfo','web_hotshoolmx','web_teammx');", new HashMap<>());
//                log.debug("查询结果{}", maps);
                List<Map<String, Object>> maps1 = sitemapService.GetByCondition(s, new HashMap<>());
//                Map<String, Object> resultMap = maps.stream()
//                        .collect(Collectors.toMap(
//                                map -> (String) map.get("db_field_name"),
//                                map -> (Object) map.get("db_field_txt"),
//                                (existingValue, newValue) -> existingValue
//                        ));


//                maps1.add(resultMap);

                return maps1;
            } catch (Exception e) {
                log.warn("查询失败{}", e.getMessage());
                return null;
            }
        };
    }
}
